﻿using System;
using nRoute.Utilities;

namespace nRoute.Components.Composition
{
    public class StaticResourceLocator
        : ResourceLocatorBase
    {
        private Object _resource;

        public StaticResourceLocator(Object resource) 
        {
            Guard.ArgumentNotNull(resource, "resource");
            _resource = resource;
        }

#region overriders

        protected override object GetInstance(ITypeBuilder typeBuilder)
        {
            return _resource;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                if (_resource != null && _resource is IDisposable)
                {
                    var _disposableResource = _resource as IDisposable;
                    if (_disposableResource != null) _disposableResource.Dispose();
                    _disposableResource = null;
                }
                _resource = null;
            }
        }

#endregion

    }
}